package com.wpml.ware.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wpml.common.util.PageUtils;
import com.wpml.common.util.Query;
import com.wpml.common.util.ResultJson;
import com.wpml.product.api.ProductFeignClient;
import com.wpml.ware.dao.WareSkuDao;
import com.wpml.ware.entity.WareSkuModel;
import com.wpml.ware.service.WareSkuService;
import com.wpml.ware.vo.SkuHasStockVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 商品库存(WmsWareSku)表服务实现类
 *
 * @author makejava
 * @since 2020-04-07 12:34:53
 */
@Service
@Slf4j
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuModel> implements WareSkuService {
    @Resource
    WareSkuDao wareSkuDao;

    @Resource
    private ProductFeignClient productFeignClient;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        /**
         * skuId: 1
         * wareId: 2
         */
        QueryWrapper<WareSkuModel> queryWrapper = new QueryWrapper<>();
        String skuId = (String) params.get("skuId");
        if (!StringUtils.isEmpty(skuId)) {
            queryWrapper.eq("sku_id", skuId);
        }

        String wareId = (String) params.get("wareId");
        if (!StringUtils.isEmpty(wareId)) {
            queryWrapper.eq("ware_id", wareId);
        }


        IPage<WareSkuModel> page = this.page(
                new Query<WareSkuModel>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public void addStock(Long skuId, Long wareId, Integer skuNum) {
        //1、判断如果还没有这个库存记录新增
        List<WareSkuModel> entities = wareSkuDao.selectList(new QueryWrapper<WareSkuModel>().eq("sku_id", skuId).eq("ware_id", wareId));
        if (entities == null || entities.size() == 0) {
            WareSkuModel skuEntity = new WareSkuModel();
            skuEntity.setSkuId(skuId);
            skuEntity.setStock(skuNum);
            skuEntity.setWareId(wareId);
            skuEntity.setStockLocked(0);
            //TODO 远程查询sku的名字，如果失败，整个事务无需回滚
            //1、自己catch异常
            //TODO 还可以用什么办法让异常出现以后不回滚？高级
            try {
                ResultJson info = productFeignClient.info(skuId);
                Map<String, Object> data = (Map<String, Object>) info.getData();

                if (info.getStatus() == 200) {
                    skuEntity.setSkuName((String) data.get("skuName"));
                }
            } catch (Exception e) {
                log.warn("远程调用异常", e);
            }

            wareSkuDao.insert(skuEntity);
        } else {
            wareSkuDao.addStock(skuId, wareId, skuNum);
        }

    }

    // 检查是否有库存
    @Override
    public List<SkuHasStockVo> checkHasStock(List<Long> skuIds) {

        return skuIds.stream().map(skuId -> {
            SkuHasStockVo hasStock = new SkuHasStockVo();

            Long count = baseMapper.getSkuStock(skuId);
            hasStock.setSkuId(skuId);
            hasStock.setHasStock(count != null && count > 0);

            return hasStock;
        }).collect(Collectors.toList());
    }
}
